package pl.matt.media.extractor.impl;

import java.io.IOException;

import pl.matt.media.extractor.FramesExtractor;

/**
 * dodatkowe opcje mplayera
 * 
 *        jpeg
              Output each frame into a JPEG file in the current directory.  Each file takes the frame number padded with leading zeros as name.
                 [no]progressive
                      Specify standard or progressive JPEG (default: noprogressive).
                 [no]baseline
                      Specify use of baseline or not (default: baseline).
                 optimize=<0-100>
                      optimization factor (default: 100)
                 smooth=<0-100>
                      smooth factor (default: 0)
                 quality=<0-100>
                      quality factor (default: 75)
                 outdir=<dirname>
                      Specify the directory to save the JPEG files to (default: ./).
                 subdirs=<prefix>
                      Create numbered subdirectories with the specified prefix to save the files in instead of the current directory.
                 maxfiles=<value> (subdirs only)
                      Maximum number of files to be saved per subdirectory.  Must be equal to or larger than 1 (default: 1000).
 * 
 * 
 * zobacz też:
 * http://beradrian.wordpress.com/2008/01/30/jmplayer/
 * 
 * @author mateusz
 * 
 */
public class MplayerFramesExtractor implements FramesExtractor {

	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MplayerFramesExtractor.class);

	private static final String MENCODER_COMMAND = "mplayer";
	
	// możę być też png
	private String format = "jpeg";

	public boolean extractFrames(String videoFilePath, String outputDirectoryPath) {
		String cmd = MENCODER_COMMAND + " -vo " + format + ":outdir=" + outputDirectoryPath + " " + videoFilePath;
		Runtime runtime = Runtime.getRuntime();
		LOG.debug(cmd + " <-- cmd");
		try {
			Process process = runtime.exec(cmd);
			//String result = IOUtils.toString(process.getInputStream());
			int value = process.waitFor();
			LOG.debug(value + " <-- value");
			return value == 0;
		} catch (IOException e) {
			e.printStackTrace();
			LOG.warn("Musisz mieć zainstalowany program mplayer");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
